package com.srd.phone.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.shengrongdai.srd.R;
import com.srd.phone.bean.SucceedLoanDetail;
import com.srd.phone.mvp.presenters.UtilPresenter;
import com.srd.phone.utils.Constants;
import com.srd.phone.utils.wheel.widget.PopupDialogView;

/**
 * 还款中的借款标详情
 */
public class SucceedLoanDetailActivity extends BaseActivity {

    private LinearLayout ll_layout;
    private TextView tv_title_name, tv_type, tv_money, tv_interest_rate, tv_interest,
            tv_num, tv_date, tv_method, tv_progress;

    private UtilPresenter utilPresenter = null;

    private int id = 0;  // 24

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_succeed_loan_detail);

        initView();
        initData();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void initView() {
        tv_common_title.setText("还款中的借款标详情");

        ll_layout = (LinearLayout)findViewById(R.id.ll_layout);
        Intent intent = getIntent();
        String flag = intent.getStringExtra(Constants.SUCCEEDLOAN);
        id = intent.getIntExtra("detail_id", 0);
        if(flag.equals("26")){
            ll_layout.setVisibility(View.GONE);
        }else{
            ll_layout.setVisibility(View.VISIBLE);
            findViewById(R.id.btn_return_money).setOnClickListener(new View.OnClickListener() {  // 还款
                @Override
                public void onClick(View v) {
                    new PopupDialogView(SucceedLoanDetailActivity.this).show();
                }
            });

            findViewById(R.id.btn_recharge).setOnClickListener(new View.OnClickListener() {  // 充值
                @Override
                public void onClick(View v) {

                }
            });
        }

        tv_title_name = (TextView)findViewById(R.id.tv_title_name);
        tv_type = (TextView)findViewById(R.id.tv_type);
        tv_money = (TextView)findViewById(R.id.tv_money);
        tv_interest_rate = (TextView)findViewById(R.id.tv_interest_rate);
        tv_interest = (TextView)findViewById(R.id.tv_interest);
        tv_num = (TextView)findViewById(R.id.tv_num);
        tv_date = (TextView)findViewById(R.id.tv_date);
        tv_method = (TextView)findViewById(R.id.tv_method);
        tv_progress = (TextView)findViewById(R.id.tv_progress);
        tv_progress.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tv_progress.getPaint().setAntiAlias(true);//抗锯齿

    }

    public void initData() {

        showWaitingDialog();
        presenter = new UtilPresenter(requestQueue, this);
        utilPresenter = (UtilPresenter)presenter;
        setUrlArrs(Constants.MYBMARK_INFO);
        getData();

        tv_progress.setOnClickListener(new View.OnClickListener() {  // 查看合同
            @Override
            public void onClick(View v) {
//                gotoActivity(CheckContractActivity.class);   // 老版本 图片;
                Intent intent = new Intent(SucceedLoanDetailActivity.this, WebViewActivity.class);
//                intent.putExtra("urlValue", );   // url value
                intent.putExtra("titleName", "查看合同");
                startActivity(intent);
            }
        });
    }

    private void getData(){
        utilPresenter.accountDetail(Constants.MYBMARK_INFO, id);
    }

    @Override
    public void returnData(String tag, String data) {
        super.returnData(tag, data);
        cancelWaitingDialog();
        if(Constants.MYBMARK_INFO.equals(tag)){
            SucceedLoanDetail info = SucceedLoanDetail.parseData(data);
            if(info == null)  return;
            tv_title_name.setText(info.nm);
            tv_type.setText(info.tidm);
            tv_money.setText(info.moneys + "元");
            tv_interest_rate.setText(info.anyield + "%");
            tv_interest.setText(info.profit + "元");
//            tv_num.setText("");  // 第几期
            tv_date.setText(info.stime);
            tv_method.setText(info.numbersm);
        }
    }

    @Override
    public void onError(String tag, VolleyError error) {
        super.onError(tag, error);
        cancelWaitingDialog();
    }
}